<template>
  <el-dialog :title="info.title" v-if="visible" :visible.sync="visible" center width="760px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="120px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="12">
          <el-form-item style="width: 100%" label="委托公司" prop="entrust_id">
            <el-select v-model="form.entrust_id" size="small" style="width: 100%">
              <el-option
                v-for="item in sels"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="委托代理人" prop="agent" style="width: 100%">
            <el-input v-model="form.agent" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" prop="phone" style="width: 100%">
            <el-input v-model="form.phone" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="履行地点" prop="addr" style="width: 100%">
            <el-input v-model="form.addr" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="签订日期" prop="sign_time" style="width: 100%">
            <el-date-picker
              v-model="form.sign_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="最终优惠价" prop="price" style="width: 100%">
            <el-input v-model="form.price" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="税率" prop="tax" style="width: 100%">
            <el-input v-model="form.tax" size="small" clearable style="width: 90%" /> %
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="货运至" prop="place" style="width: 100%">
            <el-input v-model="form.place" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收货人" prop="person" style="width: 100%">
            <el-input v-model="form.person" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="project" style="width: 100%">
            <el-input v-model="form.project" size="small" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item style="width: 100%" label="付款方式" prop="paytype">
            <el-select v-model="form.paytype" size="small" style="width: 100%">
              <el-option
                v-for="item in paytypes"
                :key="item.key"
                :label="item.value"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交货期" prop="delivery_time" style="width: 100%">
            <el-date-picker
              v-model="form.delivery_time"
              type="date"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              clearable
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="outExcel">合 同</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { outHT, entrusts } from '@/api/purchase'
import { contract } from '@/api/purchase/purchase'
import { payments } from '@/api/supplier'

export default {
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      form: {},
      entrust: {},
      sels: [],
      paytypes: [],
      rules: {
        agent: [{ required: true, trigger: 'blur', message: '委托代理人不可为空' }],
        price: [{ required: true, trigger: 'blur', message: '最直终优惠价不可为空' }],
        paytype: [{ required: true, trigger: 'blur', message: '付款方式不可为空' }],
        phone: [{ required: true, trigger: 'blur', message: '电话不可为空' }],
        entrust_id: [{ required: true, trigger: 'blur', message: '委托公司不可为空' }],
        addr: [{ required: true, trigger: 'blur', message: '履行地点不可为空' }],
        sign_time: [{ required: true, trigger: 'blur', message: '签订日期不可为空' }],
        tax: [{ required: true, trigger: 'blur', message: '税率不可为空' }],
        delivery_time: [{ required: true, trigger: 'blur', message: '交货期不可为空' }],
        project: [{ required: true, trigger: 'blur', message: '项目名称不可为空' }],
        person: [{ required: true, trigger: 'blur', message: '收货人不可为空' }],
        place: [{ required: true, trigger: 'blur', message: '货运至不可为空' }]
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'entrust', {})
        this.form = this.info.edit
        this.$set(this.form, 'phone', '0512-67688835')
        this.contract()
        this.entrusts()
        this.payments()
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    contract() {
      contract({ pur_id: this.form.pur_id }).then(response => {
        const info = response.info
        for (const v in info) {
          if (v === 'paytype') {
            this.$set(this.form, v, decodeURIComponent(info[v]))
          } else {
            this.$set(this.form, v, info[v])
          }
        }
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    entrusts() {
      entrusts({}).then(response => {
        this.sels = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    payments() {
      payments({}).then(response => {
        this.paytypes = response.list
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    outExcel() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          this.$set(this.form, 'paytype', encodeURIComponent(this.form.paytype))
          this.$set(this.form, 'infos', '')
          outHT({
            ...this.form
          }).then(response => {
            const blob = new Blob([response.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
            const downloadElement = document.createElement('a')
            const href = window.URL.createObjectURL(blob)
            downloadElement.href = href
            downloadElement.download = decodeURI(response.headers['filename'])
            document.body.appendChild(downloadElement)
            downloadElement.click()
            document.body.removeChild(downloadElement)
            window.URL.revokeObjectURL(href)
            this.getData()
            this.handleCancel()
            this.$message.success('导出成功')
          }).catch(err => {
            this.$message.error(err.msg)
          })
        }
      })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
